﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using System.Threading;

namespace SchoolManagementSystem.Forms
{
    /// <summary>
    /// Loading form before start application
    /// Developer: Linh Pham
    /// Country: VIET NAM
    /// </summary>
    public partial class frmLoading : Form
    {
        BackgroundWorker m_oWorker;

        public bool Status;

        public frmLoading()
        {
            InitializeComponent();
            InitWorker();
        }

       
        void InitWorker()
        {
            progressBar1.EditValue = 0;
            txtpercent.Text = "";
            m_oWorker = new BackgroundWorker();
            m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
            m_oWorker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged);
            m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_oWorker_RunWorkerCompleted);
            m_oWorker.WorkerReportsProgress = true;
            m_oWorker.WorkerSupportsCancellation = true;
            m_oWorker.RunWorkerAsync();
        }

        /// <summary>
        /// On completed do the appropriate task
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void m_oWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            
            if (e.Error != null)
            {
               txtpercent.Text = "Có lỗi xảy ra trong quá trình nạp thư viện, vui lòng thử lại";
               this.DialogResult = System.Windows.Forms.DialogResult.No;
            }
            else
            {
                
                m_oWorker.Dispose();

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        /// <summary>
        /// Notification is performed here to the progress bar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.EditValue = e.ProgressPercentage;
            txtpercent.Text = "Processing......" + progressBar1.EditValue.ToString() + "%";
        }

       
        void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            //do tốc độ load thư viện khá nhanh (<1s) nên tạm thời dùng thread để tạo độ trễ cho đẹp
            m_oWorker.ReportProgress(10);
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("vi-VN");
            m_oWorker.ReportProgress(20);
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("vi-VN");
            m_oWorker.ReportProgress(30);
            DevExpress.XtraGrid.Localization.GridLocalizer.Active = new VietnameseGridLocalizer();
            m_oWorker.ReportProgress(40);
            DevExpress.XtraEditors.Controls.Localizer.Active = new VietnameseEditorsLocalizer();
            m_oWorker.ReportProgress(50);
            DevExpress.UserSkins.BonusSkins.Register();
            m_oWorker.ReportProgress(60);
            Application.EnableVisualStyles();
            m_oWorker.ReportProgress(70);
            Application.SetCompatibleTextRenderingDefault(false);
            m_oWorker.ReportProgress(80);
            RegisterService.RegisterServices.RegisterDependencyResolver();
            m_oWorker.ReportProgress(90);
            m_oWorker.ReportProgress(100);
        }

     
    }
}
